package org.framework.sys;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import net.pgia.pub.util.common.StringUtil;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;


/**
 * Title: 服务开通系统<br>
 * Description: <br>
 * Date: 2007-9-22 <br>
 * Copyright (c) 2007 CATTSoft<br>
 *
 * @author liaoyh
 */
public class LogConfig {
    private static final Logger log = Logger.getLogger(LogConfig.class);

    /**
     * 全路径名，如./conf/log4j-crawl.properties
     */
    private String propFile;
    
    public LogConfig() {
		// TODO Auto-generated constructor stub
	}
    
    public void init() {

        Properties config = new Properties();
        InputStream is = null;
        try {
        	//优先使用程序设置的propFileName
        	if(StringUtil.isBlank(propFile)){
        		propFile = System.getProperty("log4j");
        	}
            
            System.out.println("log4j's config =" + propFile);//log4j初始化前，只能System.out输出
            
            if (!StringUtil.isBlank(propFile)) {
                PropertyConfigurator.configure(propFile);// 加载传入的配置文件
            } else {
                is = LogConfig.class.getClassLoader().getResourceAsStream("log4j.properties");//加载默认位置的配置文件
                config.load(is);
            }

        } catch (IOException e) {
            throw new RuntimeException("failed to read log4j configuration file");
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                }
            }
        }
        PropertyConfigurator.configure(config);
        log.debug("can log");
    }

	public String getPropFile() {
		return propFile;
	}

	public void setPropFile(String propFile) {
		this.propFile = propFile;
	}

}
